import { Photo } from "../entity/Photo"
import { PhotoMetadata } from "../entity/PhotoMetadata"
import { AppDataSource } from "../data-source"


export const createRelation = async () => { 
  

  // create a photo
  const photo = new Photo()
  photo.name = "Me and Bears"
  photo.description = "I am near polar bears"
  photo.filename = "photo-with-bears.jpg"
  photo.views = 1
  photo.isPublished = true

  // create a photo metadata
  const metadata = new PhotoMetadata()
  metadata.height = 640
  metadata.width = 480
  metadata.compressed = true
  metadata.comment = "cybershoot"
  metadata.orientation = "portrait"
  metadata.photo = photo // this way we connect them

  // get entity repositories
  const photoRepository = AppDataSource.getRepository(Photo)
  const metadataRepository = AppDataSource.getRepository(PhotoMetadata)

  // first we should save a photo
  await photoRepository.save(photo)

  // photo is saved. Now we need to save a photo metadata
  await metadataRepository.save(metadata)

  // done
  console.log(
    "Metadata is saved, and the relation between metadata and photo is created in the database too",
  )
}

// 查询单个依赖项目
export const findRelation =async () => { 
  // const photoRepository = AppDataSource.getRepository(Photo)
  // const photos = await photoRepository.find({
  //   relations: {
  //     metadata: true,
  //   },
  // })
  // console.log('photos :>> ', photos);

  // const photoMetadata = AppDataSource.getRepository(PhotoMetadata)
  // const metadatas = await photoMetadata.find({
  //   relations: { photo: true }
  // })
  // console.log('metadatas :>> ', metadatas);


  const photos = await AppDataSource.getRepository(Photo)
    .createQueryBuilder("photo")
    .innerJoinAndSelect("photo.metadata", "metadata")
    .getMany()
  console.log('photos :>> ', photos);
}

export const cascadeInsert = async () => {
  
  const photo = new Photo()
  photo.name = "Me and Bears"
  photo.description = "I am near polar bears"
  photo.filename = "photo-with-bears.jpg"
  photo.isPublished = true
  photo.views = 1

  // create photo metadata object
  const metadata = new PhotoMetadata()
  metadata.height = 640
  metadata.width = 480
  metadata.compressed = true
  metadata.comment = "cybershoot"
  metadata.orientation = "portrait"

  photo.metadata = metadata // this way we connect them

  // get repository
  const photoRepository = AppDataSource.getRepository(Photo)

  // saving a photo also save the metadata
  await photoRepository.save(photo)

  console.log("Photo is saved, photo metadata is saved too.")
  
}